package com.iotek.bookstore.modle.dao.impl;

import com.iotek.bookstore.modle.entity.BookContent;
import com.iotek.bookstore.util.PreparedStatementSetter;

import java.sql.PreparedStatement;
import java.sql.SQLException;

public class BookContentDaoImpl extends MyBookstoreDaoImpl<BookContent> {
    @Override
    public int insert(final BookContent bookContent) {
        String sql="insert into bookdetails(bookDetailsId,printingTime,print,ISBN,words,pages,format,sheet,pack,contentIntro,writerIntro,catalog,mediaComments,excerpt)" +
            " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        return this.update(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement pstmt) throws SQLException {
                pstmt.setInt(1,bookContent.getBookdetailsId());
                pstmt.setString(2,bookContent.getPrintingTime());
                pstmt.setInt(3,bookContent.getPrint());
                pstmt.setString(4,bookContent.getISBN());
                pstmt.setInt(5,bookContent.getWords());
                pstmt.setInt(6,bookContent.getPages());
                pstmt.setString(7,bookContent.getFormat());
                pstmt.setString(8,bookContent.getSheet());
                pstmt.setString(9,bookContent.getPack());
                pstmt.setString(10,bookContent.getContentIntro());
                pstmt.setString(11,bookContent.getWriterIntro());
                pstmt.setString(12,bookContent.getCatalog());
                pstmt.setString(13,bookContent.getMediaComments());
                pstmt.setString(14,bookContent.getExcerpt());
            }
        });
    }

    /*
    * 根据图书ID删除图书信息
    * */
    @Override
    public int delete(final int id) {
        String sql="delete from bookdetails where bookDetailsId=?";
        return this.update(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement pstmt) throws SQLException {
                pstmt.setInt(1,id);
            }
        });
    }

    /*
    * 根据图书ID更改图书信息
    * */
    @Override
    public int update(final BookContent bookContent) {
        String sql="update  bookdetails set printingTime=?,print=?,ISBN=?,words=?,pages=?,format=?," +
            "sheet=?,pack=?,contentIntro=?,writerIntro=?,catalog=?,mediaComments=?,excerpt=? where bookDetailsId=?";
        return this.update(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement pstmt) throws SQLException {
                pstmt.setString(1,bookContent.getPrintingTime());
                pstmt.setInt(2,bookContent.getPrint());
                pstmt.setString(3,bookContent.getISBN());
                pstmt.setInt(4,bookContent.getWords());
                pstmt.setInt(5,bookContent.getPages());
                pstmt.setString(6,bookContent.getFormat());
                pstmt.setString(7,bookContent.getSheet());
                pstmt.setString(8,bookContent.getPack());
                pstmt.setString(9,bookContent.getContentIntro());
                pstmt.setString(10,bookContent.getWriterIntro());
                pstmt.setString(11,bookContent.getCatalog());
                pstmt.setString(12,bookContent.getMediaComments());
                pstmt.setString(13,bookContent.getExcerpt());
                pstmt.setInt(14,bookContent.getBookdetailsId());
            }
        });
    }
}
